package org.linkerproject.browser;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.LocationAdapter;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.OpenWindowListener;
import org.eclipse.swt.browser.TitleEvent;
import org.eclipse.swt.browser.TitleListener;
import org.eclipse.swt.browser.WindowEvent;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Shell;
import org.imlinker.util.StringUtils;

public class BrowserWindow {

	protected Shell shell;

	private BrowserManager manager;

	private Browser browser;

	private String title = "";

	public String lastUrl = "";

	public BrowserWindow(Shell shell, BrowserManager manager) {

		this.manager = manager;
		this.shell = shell;
		try {
			open();
		} catch (Exception e) {
			e.printStackTrace();
		}

		initListener();
	}

	public void browse(String url) {
		browser.setUrl(url);
		browser.forceFocus();
	}

	/**
	 * Create contents of the window
	 */
	protected void createContents() {
		shell.setLayout(new FillLayout());
		shell.setText("JinDou Cloud");

		browser = new Browser(shell, SWT.NONE);
	}

	public String getTitle() {

		return title;
	}

	private void initListener() {
		shell.addFocusListener(new FocusAdapter() {

			@Override
			public void focusGained(FocusEvent e) {
				browser.forceFocus();
			}

		});

		browser.addKeyListener(new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent e) {
				// Goto dialog ctrl+d
				if (e.stateMask == SWT.CTRL && e.keyCode == 100) {
					manager.gotoDialog();
				} else if (e.stateMask == SWT.CTRL && e.keyCode == 98) {
					// ctrl + b
					if (browser.isBackEnabled()) {
						browser.back();
					}
				} else if (e.stateMask == SWT.CTRL && e.keyCode == 103) {
					// ctrl + g
					if (browser.isForwardEnabled()) {
						browser.forward();
					}
				} else if (e.stateMask == SWT.CTRL && e.keyCode == 102) {
					// ctrl + f
					manager.findDialog();
				} else if (e.stateMask == SWT.CTRL && e.keyCode == 101) {
					// ctrl + e
					manager.gotoIndex();
				}
			}
		});

		browser.addTitleListener(new TitleListener() {

			@Override
			public void changed(TitleEvent e) {
				shell.setText(e.title + " -- Jindou Cloud");
				title = e.title;
			}

		});

		browser.addLocationListener(new LocationAdapter() {

			@Override
			public void changed(LocationEvent e) {
				lastUrl = e.location;
				System.out.println(e.location);

			}

			@Override
			public void changing(LocationEvent e) {

				String aboutLabel = "about:blank";
				if (e.location.startsWith(aboutLabel)) {
					browser.stop();
				}

				String fileLabel = "file://";
				if (lastUrl.startsWith(fileLabel)) {
					manager.save(lastUrl.substring(fileLabel.length()), browser
							.getText());
				}

			}

		});

		browser.addOpenWindowListener(new OpenWindowListener() {

			@Override
			public void open(final WindowEvent e) {

				e.browser = new Browser(shell, SWT.NONE);
				e.browser.addLocationListener(new LocationAdapter() {

					@Override
					public void changing(LocationEvent le) {
						BrowserWindow.this.browse(le.location);
						e.browser.dispose();
					}

				});
			}
		});

	}

	/**
	 * Open the window
	 */
	private void open() {
		createContents();
		browser.forceFocus();
	}

	public void refresh() {
		browser.refresh();
	}

	public void setHtml(String pageHtmlStr) {
		if (StringUtils.isNotEmpty(pageHtmlStr)) {
			browser.setText(pageHtmlStr);
		}
	}

}
